const alert = document.querySelector(".alert");
function alertShow(msg, backgroundcolor, color) {
  alert.classList.add("show");
  alert.innerHTML = msg;
  alert.style.color = color;
  alert.style.backgroundColor = backgroundcolor;
  setTimeout(function () {
    alert.classList.remove("show");
  }, 2000);
}

// 登录函数
var subBtn = document.querySelector(".submitBtn");
subBtn.onclick = function () {
  var form = document.getElementById("form");
  const email = document.querySelector(".username").value;
  const password = document.querySelector(".password").value;
  //  获取提示框

  if (email == "" || password == "") {
    return alertShow("邮箱地址或密码为空！");
  } else if (!reg.test(email)) {
    return alertShow("邮箱格式错误");
  }
  if (password.length < 6) {
    return alertShow("密码不能小于6位");
  }
  $.ajax({
    url: `http://110.40.139.172:43120/users/login?email=${email}&password=${password}`,
    type: "GET",
    success: function (result) {
      if (result.code == 0) {
        localStorage.setItem("token", result.token);
        window.location.replace("backStage.html");
      } else {
        alertShow(result.message);
      }
    },
    error: function (error) {
      console.log(error);
    },
  });
};
// 获取验证码的函数
// 倒计时
function settime(el) {
  var registerE = document.querySelector(".registerE").value;
  if (!reg.test(registerE)) {
    return registerAlertShow("邮箱格式错误");
  }
  if (count == 0) {
    el.removeAttribute("disabled");
    el.innerHTML = "获取验证码";
    alertShow("发送成功", "#bfe8c0", "#0bb767");
    count = 60;
    return;
  } else {
    el.setAttribute("disabled", true);
    el.innerHTML = count + "s 重新发送";
    count--;
  }
  setTimeout(function () {
    settime(el);
  }, 1000);
}
var count = 60;
var codeBtn = document.querySelector(".codeBtn");
var registerPwd = document.querySelector(".registerPwd").value;
codeBtn.onclick = function () {
  var registerAlert = document.querySelector(".register-alert");
  var registerE = document.querySelector(".registerE").value;
  //数据校验;
  //警告
  function registerAlertShow(msg) {
    registerAlert.classList.add("show");
    registerAlert.innerHTML = msg;
    setTimeout(function () {
      registerAlert.classList.remove("show");
    }, 2000);
  }
  if (registerE == "") {
    return registerAlertShow("邮箱为空");
  }
  $.ajax({
    url: "http://110.40.139.172:43120/users/getCode",
    type: "post",
    data: {
      email: registerE,
    },
    success: function (result) {
      settime(codeBtn);
    },
    error: function (error) {
      console.log(error);
    },
  });
};
// 注册函数\w+@\w+(\.\w+)+
var registerAlert = document.querySelector(".register-alert");
function registerAlertShow(msg) {
  registerAlert.classList.add("show");
  registerAlert.innerHTML = msg;
  setTimeout(function () {
    registerAlert.classList.remove("show");
  }, 2000);
}
var registerBtn = document.querySelector(".registerBtn");
let reg =
  /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
registerBtn.onclick = function () {
  var code = document.querySelector(".code").value;
  var registerE = document.querySelector(".registerE").value;
  var registerPwd = document.querySelector(".registerPwd").value;

  if (registerE == "" || registerPwd == "") {
    return registerAlertShow("邮箱地址或密码为空");
  } else if (!reg.test(registerE)) {
    return registerAlertShow("邮箱格式错误");
  } else if (code == "") {
    return registerAlertShow("请输入验证码");
  }
  $.ajax({
    url: "http://110.40.139.172:43120/users/register",
    type: "post",
    data: {
      email: registerE,
      password: registerPwd,
      code: code,
    },
    success: function (result) {
      if (code == 0) {
        alert(result.message);
      } else {
        registerAlertShow(result.message);
      }
    },
    error: function (error) {
      console.log(error);
    },
  });
};

// 动画
var pannerRegister = document.querySelector(".panner-register");
var signUp = document.querySelector(".signup");
var register = document.querySelector(".register");
pannerRegister.onclick = function () {
  let code = document.querySelector(".code");
  let registerE = document.querySelector(".registerE");
  let registerPwd = document.querySelector(".registerPwd");

  code.value = "";
  registerE.value = "";
  registerPwd.value = "";

  signUp.classList.add("active");
  register.classList.add("active");
  register.classList.remove("nodisplay");
  signUp.classList.add("nodisplay");
};
var pannerLogin = document.querySelector(".panner-login");
pannerLogin.onclick = function () {
  let email = document.querySelector(".username");
  let password = document.querySelector(".password");
  email.value = "";
  password.value = "";
  signUp.classList.remove("active");
  register.classList.remove("active");
  register.classList.add("nodisplay");
  signUp.classList.remove("nodisplay");
};

// 密码显示
let eye = document.getElementById("eye");

let flag = 0;
eye.onclick = function () {
  let input = document.querySelector(".input.password");
  if (flag == 0) {
    input.type = "text";
    flag = 1;
    eye.innerHTML = "&#xebcc;";
  } else {
    input.type = "password";
    eye.innerHTML = "&#xebcd;";
    flag = 0;
  }
};
let f = 0;
let regeye = document.getElementById("regeye");
regeye.onclick = function () {
  let input = document.querySelector(".registerPwd");
  if (f == 0) {
    input.type = "text";
    f = 1;
    regeye.innerHTML = "&#xebcc;";
  } else {
    input.type = "password";
    regeye.innerHTML = "&#xebcd;";
    f = 0;
  }
};
